package com.linked.basemanage.feedback.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.linked.basemanage.feedback.bean.dto.FeedBackDTO;
import com.linked.basemanage.feedback.bean.param.FeedBackPageParam;
import com.linked.basemanage.feedback.bean.po.FeedBackInfoPO;
import com.linked.basemanage.feedback.bean.vo.FeedBackVO;
import com.linked.basemanage.feedback.service.IFeedBackService;
import com.linked.basemanage.file.service.IFileHandleService;
import com.linked.universal.common.LinkedPrompt;
import com.linked.universal.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/feedback")
public class FeedBackController {

    private final IFeedBackService feedBackService;

    private final ObjectMapper mapper;

    private final IFileHandleService fileHandleService;

    public FeedBackController(IFeedBackService feedBackService, ObjectMapper mapper,
                              IFileHandleService fileHandleService) {
        this.feedBackService = feedBackService;
        this.mapper = mapper;
        this.fileHandleService = fileHandleService;
    }



    @PostMapping("/addFeedBack")
    Result addFeedBack(@RequestBody FeedBackDTO param) throws Exception {
        if (log.isInfoEnabled()) {
            log.info("添加反馈接口 ----- 入参：{}", mapper.writeValueAsString(param));
        }
        boolean ret = false;
        try {
            String json_str = mapper.writeValueAsString(param);
            FeedBackInfoPO baseInfo = mapper.readValue(json_str, FeedBackInfoPO.class);
            ret = feedBackService.addFeedBack(baseInfo);
            if (!CollectionUtils.isEmpty(param.getFileIds())) {
                fileHandleService.setFilesBelong(param.getFileIds(), baseInfo.getFeedbackId(), "basemanage_feedback_info");
            }
        } catch (Exception e) {
            log.error("添加反馈接口 ----- 异常", e);
            return Result.error(LinkedPrompt.ERROR_MESSAGE);
        }
        return ret ? Result.success() : Result.ok(false, LinkedPrompt.ADD_FAILURE);
    }

    @PostMapping("queryFeedBackPage")
    Result queryFeedBackPage(@RequestBody FeedBackPageParam param) throws Exception {
        if (log.isInfoEnabled()) {
            log.info("查询反馈页面接口 ----- 入参：{}", mapper.writeValueAsString(param));
        }

        IPage<FeedBackVO> page = null;
        try {
            if (CollectionUtils.isEmpty(param.getOrders())) {
                List<OrderItem> orderItemList = new ArrayList<>();
                OrderItem firstOrder = new OrderItem();
                firstOrder.setColumn("fi.create_time");
                firstOrder.setAsc(false);
                orderItemList.add(firstOrder);
                param.setOrders(orderItemList);
            }
            page = feedBackService.queryFeedBackPage(param);
        } catch (Exception e) {
            log.error("查询反馈页面接口 ----- 异常", e);
            return Result.error(LinkedPrompt.ERROR_MESSAGE);
        }
        return Result.success(page);
    }

    @PostMapping("relveFeedBack")
    Result relveFeedBack(String feedBackId, String userId, String remarks) {
        if (log.isInfoEnabled()) {
            log.info("解决反馈接口 ----- 入参：" + feedBackId);
        }
        try {
            FeedBackInfoPO info = new FeedBackInfoPO();
            info.setFeedbackId(feedBackId);
            info.setFeedbackStatus(0);
            info.setSolveRemarks(remarks);
            info.setSolveUserId(userId);
            info.setSolveTime(LocalDateTime.now());
            feedBackService.relveFeedBack(info);
        } catch (Exception e) {
            log.error("解决反馈接口 ----- 异常", e);
            return Result.error(LinkedPrompt.ERROR_MESSAGE);
        }
        return Result.success();
    }


}
